 //19.删除链表的倒数第N个结点
 //https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
class Solution {
    int ListLength(ListNode* head)
    {
        int ret = 0;
        while(head)
        {
            ++ret;
            head = head->next;
        }
        return ret;
    }
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* root = new ListNode(0,head); //构造哨兵节点
        int length = ListLength(head);
        ListNode* cur = root;
        //删除倒数第n个节点 就是删除第 l-n 个节点 找到其前驱l-n+1即可 
        for(int i = 1;i<length-n+1;++i,cur = cur->next);
        ListNode* delNode = cur->next;
        cur->next = cur->next->next;
        delete delNode;
        ListNode* ret = root->next;
        delete root;
        return ret;
    }
};